from PIL import Image, ImageChops, ImageEnhance
import matplotlib.pyplot as plt
import requests
from flask import Blueprint, jsonify, request
from server.resource.util.ApiResponse import ApiResponse

bp = Blueprint("A4", __name__, url_prefix="/A4")

@bp.route('/get', methods=['POST'])
def DCT():
    try:
        data = request.get_json()
        image_url = data.get('image_url')
        print(1)
        if not image_url:
            raise ValueError("No image_url provided in the request")
        print(2)
        api_url = "http://api.tp.zzzsleep.icu/A4/get"
        response = requests.post(api_url, json={'image_url': image_url})  # 发送JSON数据
        print(response.json())
        if response.status_code == 200:
            print(11)
            response_data = response.json()
            url = response_data.get('data')
            if url:
                print(f"Image uploaded successfully! URL: {url}")
                return jsonify(ApiResponse.success(url))
            else:
                return jsonify(ApiResponse.error("No image URL returned by the API"))
        else:
            return jsonify(ApiResponse.error(f"API request failed with status code {response.status_code}"))
    except Exception as e:
        print(f"An error occurred: {e}")

        return jsonify(ApiResponse.server_error())